﻿/*
*资料盘录入
*/
Chenxin.UI.PublicHouse = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/publicHouse.ashx',
    id: 'publicHouse',
    jqmWidth: 700,
    renderTo: 'jqm_PublicHouseWin',
    initFormValidator: function() {
        var _this = this;
        //验证
        $.formValidator.initConfig({ validatorgroup: this.id, formid: "form_PublicHouse", onerror: function(msg) { alert(msg) }, onsuccess: function() { return true; } });

        //小区非空验证
        $("#Community").formValidator({
            validatorgroup: this.id,
            onshow: "请输入小区名称！",
            onfocus: "中文或拼音",
            oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "小区名称不能为空!"
            },
            onerror: "小区名称不能为空!"
        });

        //房间号非空验证
        $("#Room").formValidator({
            validatorgroup: this.id,
            onshow: "请输入房间号！",
            onfocus: "请输入房间号",
            oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if (_this.currentBuildingModel) {
                    if (_this.currentBuildingModel.RoomRule == '*' || _this.currentBuildingModel.RoomRule == '') {//独栋别墅
                        //进行AJAX验证
                        var exist = true;
                        Chenxin.ajax({
                            type: "get",
                            dataType: "json",
                            async: false,
                            url: _this.actionUrl,
                            data: { op: 'checkroom', room: val, communityid: _this.currentCommunityInfo.CommunityId, buildingid: _this.currentBuildingModel.BuildingId },
                            onSuccess: function(data) {
                                exist = data.exist;
                            }
                        });
                        if (exist) {
                            return '房间号已经存在！';
                        } else {
                            return true;
                        }
                    } else {
                        eval('s=/' + _this.currentBuildingModel.RoomRule + '/');
                        if (s.test(val)) {//进行AJAX验证
                            var exist = true;
                            Chenxin.ajax({
                                type: "get",
                                dataType: "json",
                                async: false,
                                url: _this.actionUrl,
                                data: { op: 'checkroom', room: val, communityid: _this.currentCommunityInfo.CommunityId, buildingid: _this.currentBuildingModel.BuildingId },
                                onSuccess: function(data) {
                                    exist = data.exist;
                                }
                            });
                            if (exist) {
                                return '房间号已经存在！';
                            } else {
                                return true;
                            }
                        } else {
                            return false;
                        }
                    }
                }
                return '请先选择楼栋！';
            },
            onerror: '房间号格式错误！'
        });

        //面积验证
        $("#BuildingArea").formValidator({
            validatorgroup: this.id,
            onshow: "请输入房源面积！",
            onfocus: "请输入房源面积",
            oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "面积不能为空!"
            },
            onerror: "面积不能为空!"
        }).functionValidator({ //数字验证
            fun: function(val, elem) {
                if (parseInt(val) == 0) {
                    return false;
                }
                else {
                    return val.isNum();
                }
            },
            onerror: "请输入非0数字"
        });

        //客户姓名非空验证
        $("#CustomerName").formValidator({
            validatorgroup: this.id,
            onshow: "请输入客户姓名！",
            onfocus: "请输入客户姓名",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "客户姓名不能为空!"
            },
            onerror: "客户姓名不能为空!"
        });

        $("#Mobile").formValidator({ validatorgroup: this.id,
            onshow: "请输入手机号！", onfocus: "请输入手机号", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) { if (val == "") return true; return val.isMobile(); }, onerror: "请输入正确的手机号"
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#Phone").val() == "" && $("#Mobile").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机或电话！"
        });

        $("#Phone").formValidator({ validatorgroup: this.id,
            onshow: "请输入电话！", onfocus: "请输入电话", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#Phone").val() == "" && $("#Mobile").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机或电话！"
        });
        
    },
    createForm: function() {
        return formCommon.PublicHouse('publicHouse');
    },
    createComboList: function() {
        var _this = this;
        this.region = new Chenxin.UI.ParameterCombo({
            name: 'Region',
            containerId: '#Region',
            key: 'System.Region',
            tbxWidth: 120,
            inModelType: 'txt',
            afterSetValue: function(newVal) {
                if (newVal == this.defVal) {
                    _this.block.clearItems();
                } else {
                    _this.block.param.subkey = newVal;
                    _this.block.reload(_this.currentCommunityInfo.Block);
                }
            },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    _this.block.clearItems();
                } else {
                    _this.block.param.subkey = this.getValue();
                    _this.block.reload();
                }
            }
        });
        this.block = new Chenxin.UI.ParameterCombo({
            containerId: '#Block',
            name: 'Block',
            key: 'System.Block',
            tbxWidth: 120,
            colWidth: 90,
            inModelType: 'txt',
            autoLoad: false
        });

        //楼栋
        this.building = new Chenxin.UI.Combo({
            actionUrl: 'service/CommunityBuildingMgr.ashx?op=getcombolist',
            containerId: '#BuildingName',
            typeModel: 'new',
            name: 'BuildingId',
            inModelType: 'all',
            tbxWidth: 120,
            colNum: 4,
            txtModelName: 'BuildingName',
            autoLoad: false,
			hasPage:10,
            onSelected: function() {
                _this.loadBuildingInfo(this.getValue());
            }
        });

        //房屋类型
        this.buildingType = new Chenxin.UI.ParameterCombo({
            containerId: '#BuildingType',
            name: 'BuildingType',
            key: 'Building.Type',
            inModelType: 'txt',
            autoLoad: true,
            tbxWidth: 120,
            required: true

        });
        //房屋用途
        this.houseuse = new Chenxin.UI.ParameterCombo({
            containerId: '#HouseUse',
            name: 'HouseUse',
            key: 'House.Use',
            inModelType: 'txt',
            tbxWidth: 120,
            required: true
        });

        var nationality = new Chenxin.UI.Combo({
            containerId: '#Nationality',
            name: 'Nationality',
            autoLoad: false,
            inModelType: 'txt',
            items: comboCommon.country
        });
        var combl = new Chenxin.UI.StructComboList();
        var structs = combl.comboList;
        structs.push(this.region);
        structs.push(this.block);
        structs.push(this.houseuse);
        structs.push(this.building);
        structs.push(this.buildingType);
        structs.push(nationality);
        return structs;
    },
    initEvent: function() {
        var _this = this;
        $('#publicHouse_Save').live('click', function() {
            _this.save();
        });
        $('#publicHouse_Cancle').live('click', function() {
            _this.hide();
        });
        //小区自动填充
        $('#Community').autocomplete({
            deferRequestBy: 5,
            width: 140,
            serviceUrl: this.actionUrl + '?op=loadcommunity',
            onSelect: function(value, data) {
                _this.currentCommunityInfo = data;
                _this.fitCommunityInfo();
                _this.building.param.CommunityId = _this.currentCommunityInfo.CommunityId;
                _this.building.reload();
            }
        });
    },
    loadBuildingInfo: function(modelId) {
        if (modelId == '请选择') {
            $('#span_RoomRuleRemark').remove();
            this.findByName('Room').val('');
            if (this.currentBuildingModel) {
                delete this.currentBuildingModel
            }
            return;
        }
        var _this = this;
        Chenxin.ajax({
            type: "post",
            dataType: "json",
            url: 'service/CommunityBuildingMgr.ashx?op=getmodel',
            data: { op: 'getmodel', modelid: modelId },
            onSuccess: function(data) {
                _this.currentBuildingModel = data.model;
                if ($('#span_RoomRuleRemark').length) {
                    $('#span_RoomRuleRemark').text(data.model.RoomRuleRemark);
                } else {
                    $('#Room').after('<span id="span_RoomRuleRemark" style="color:#ff0000;">' + data.model.RoomRuleRemark + '</span>');
                }
                _this.findByName('Room').val('');
                if (data.model.RoomRule == '*' || data.model.RoomRule == '') {//独栋别墅，无需填写房间号
                    $('.tr_roomnumber').hide();
                } else {
                    $('.tr_roomnumber').show();
                }
            }
        });
    },
    fitCommunityInfo: function() {
        var _data = {};
        _data.BuildingType = this.currentCommunityInfo.BuildingType;
        _data.Community = this.currentCommunityInfo.CommunityName;
        _data.HouseUse = this.currentCommunityInfo.CommunityUse;
        _data.Region = this.currentCommunityInfo.Region;
        _data.HouseAddress = this.currentCommunityInfo.Address;
        this.fitData(_data);
    },
    initComponent: function() {
        Chenxin.UI.PublicHouse.superclass.initComponent.call(this);
    }
});


/**
*上传
*/
Chenxin.UI.UploadWin = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/publicHouse.ashx',
    id: 'uploaddiv',
    renderTo: 'jqm_upload',
    createForm: function() {
        return formCommon.UploadTable();
    },
    initEvent: function() {
        var _this = this;

        $("#thumbnialUploadify").uploadify({
            uploader: 'js/uploadify.swf',
            script: 'service/publicHouse.ashx?op=fileupload',
            cancelImg: 'images/cancel.png',
            //'queueID': 'thumbnialFileQueue', //和存放队列的DIV的id一致   
            //'fileDataName'   : 'file', //和以下input的name属性一致   
            auto: false,
            multi: false,
          //  buttonImg: 'images/cancel.png',
           // buttonText: '选择',
            //'simUploadLimit': 1, //一次同步上传的文件数目
            //'sizeLimit': 19871202, //设置单个文件大小限制
            //folder: document.cookie,
            //'queueSizeLimit' : 1, //队列中同时存在的文件个数限制
            fileDesc: '支持格式:xls,xlsx', //如果配置了以下的'fileExt'属性，那么这个属性是必须的
            fileExt: '*.xls;*.xlsx', //允许的格式
            onSelect: function(event, queueID, fileObj) {
            },
            onComplete: function(event, queueID, fileObj, response, data) {
                if(response){
					try{					
						eval("var json="+response);
						if(json.success==1){
							alert(json.msg);							
						}else if(json.success==0){
							alert(json.msg);
						}else{
							alert(json.msg);
							window.location.href=Chenxin.loginUrl;
			            }
			       	}catch(e){
						alert('返回数据格式错误，文件上传失败！');
					}
				}
            },
            onError: function(event, queueID, fileObj) {
                alert("文件:" + fileObj.name + "上传失败");
            },
            onCancel: function(event, queueID, fileObj) {
                alert("取消了" + fileObj.name+"的上传");
            }
        });

        $('#startupload').live('click', function() {
            $("#thumbnialUploadify").uploadifyUpload();
            //_this.hide();
        });
        $('#stopupload').live('click', function() {
            $("#thumbnialUploadify").uploadifyClearQueue();
        });

    },
    initComponent: function() {
        Chenxin.UI.UploadWin.superclass.initComponent.call(this);
    }
});
